package starling.utils
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.system.ImageDecodingPolicy;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;
	
	
	/**
	 * 进制类型数据加载 
	 * @author lyman
	 * 
	 */	
	public class AssetLoader extends EventDispatcher
	{
		
		public function AssetLoader()
		{
			_urlStream = new URLStream();
		}
		
		private var _onProgressHandler:Function;
		private var _onCompleteHandler:Function;
		private var _extension:String;
		private var _urlStream:URLStream;
		
		public function doLoad(url:String,onCompleteHandler:Function,onProgressHandler:Function = null):void
		{
			_extension = url.split(".").pop().toLowerCase();
			if(_extension.indexOf("?") != -1)
			{
				_extension = _extension.split("?")[0];
			}
			_onCompleteHandler = onCompleteHandler;
			_onProgressHandler = onProgressHandler;
			_urlStream.position = 0;
			_urlStream.addEventListener(flash.events.IOErrorEvent.IO_ERROR, onIoError);
			_urlStream.addEventListener(flash.events.ProgressEvent.PROGRESS, onLoadProgress);
			_urlStream.addEventListener(flash.events.Event.COMPLETE, onUrlLoaderComplete);
			_urlStream.load(new URLRequest(url));
		}
		
		private function removeEvent():void
		{
			_urlStream.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
			_urlStream.removeEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			_urlStream.removeEventListener(Event.COMPLETE, onUrlLoaderComplete);
		}
		
		private function onIoError(event:flash.events.IOErrorEvent):void
		{
			removeEvent();
			_onCompleteHandler(null);
		}
		
		private function onLoadProgress(event:flash.events.ProgressEvent):void
		{
			_onProgressHandler(event.bytesLoaded / event.bytesTotal);
		}
		
		private function onUrlLoaderComplete(event:flash.events.Event):void
		{
			removeEvent();
			
			var bytes:ByteArray = new ByteArray();
			_urlStream.readBytes(bytes,0,_urlStream.bytesAvailable);
			bytes.position = 0;
			var sound:Sound;
			switch (_extension)
			{
				case "atf":
					_onCompleteHandler(bytes);
					break;
				case "fnt":
				case "xml":
					var xml:String = bytes.readUTFBytes(bytes.length);
					_onCompleteHandler(new XML(xml));
					break;
				case "mp3":
					sound = new Sound();
					sound.loadCompressedDataFromByteArray(bytes, bytes.length);
					_onCompleteHandler(sound);
					break;
				default:
					var loaderContext:LoaderContext = new LoaderContext();
					var loader:Loader = new Loader();
					loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
					loader.loadBytes(bytes, loaderContext);
					break;
			}
		}
		private function onLoaderComplete(event:Event):void
		{
			event.target.removeEventListener(flash.events.Event.COMPLETE, onLoaderComplete);
			var content:Object = event.target.content;
			
			if (content is Bitmap)
			{
				_onCompleteHandler(content as Bitmap);
			}else
			{
				_onCompleteHandler(null);
				throw new Error("Unsupported asset type: " + getQualifiedClassName(content));
			}
		}
		public function get extension():String
		{
			return _extension;
		}
	}
}